package com.tankgame.its381b;

import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author open
 */
public class InvaderSprite extends Sprite {

	//initiate random variable r
    private static Random r;
    
    //initiate width and height of sprite image
    private static final int WIDTH = 31, HEIGHT = 15;

    //create sprite with invader.png image size of width and height
    public InvaderSprite() throws IOException {
        super(Image.createImage("/res/invader.png"), WIDTH, HEIGHT);
        
        //set a reference pixel
        defineReferencePixel(getWidth() / 2, getHeight());
        
        //generate a new random int
        if( r == null ) {
            r = new Random();
        }
        
        //set randomw and randomh to random values within a range
        int randomw = (r.nextInt(Bounds.WIDTH-WIDTH));
        int randomh = (r.nextInt(10)+30);
        
        //set creation point to randomw and randomh
        setPosition(randomw, randomh);
    }

    public void move() {
      //set sprite image to next frame
      nextFrame();
      //set xdelta and ydelta to random values
      int xdelta = r.nextInt(8) - 4; // -2, -1, 0, 1, 2
      int ydelta = r.nextInt(2);     // 0, 1, 2
      
      //checks against movement outside of bounds
      if((this.getX()+xdelta)<=0) {
          xdelta = 0;
      }
      if((this.getX()+xdelta)>=(Bounds.WIDTH-WIDTH)) {
          xdelta = 0;
      }
      //move sprite
      move(xdelta, ydelta);
    }

}
